package com.data.util;

import com.data.dto.WS;
import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

public class ParserUtil {

    public static WS parseWS(String table, Map<String,Object> map){

        Object id = map.get("id");
        Document jsoup = Jsoup.parse(map.get("nr").toString());
        Elements elements = jsoup.getElementsByClass("1");
        if(elements.first() != null){
            WS ws = new WS();
            Element element = elements.first();
            if(element.getElementById("1")!=null){
                element.getElementById("1").remove();
            }
            if(element.getElementsByClass("1")!=null
                    && element.getElementsByClass("1").first()!=null
                    && element.getElementsByClass("1").first().getElementsByTag("1")!=null){
                element.getElementsByClass("1").first().getElementsByTag("1").remove();
            }
//            WordToHtmlConverter
            Elements te = element.getElementsByClass("1");
            String ti = te!=null && te.first()!=null ? te.first().text():"";
            String ay = element.getElementById("1")!=null?element.getElementById("1").text():"";
            ws.setId(Integer.parseInt(id.toString()));
            ws.setAy(ay);
            ws.setTi(ti);
            ws.setNr("<div class=\"1\">"+element.html()+"</div>");
            ws.setErr(toPDF(ti,ws.getNr()));
            return ws;
        }
        return null;
    }

    public static int toPDF(String title, String html){
        html = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\"/>\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body style=\"font-family: 宋体 !important;\">\n" +
                html.replaceAll("FONT-FAMILY: 黑体","font-family: 宋体").replaceAll("font-family: 黑体","font-family: 宋体") +
                "</body>\n" +
                "</html>";
        try {
            long start = System.currentTimeMillis();
//            ByteArrayOutputStream baos = new ByteArrayOutputStream(0xffff);
            FileOutputStream fos = new FileOutputStream(new File("C:/1//"+title+".pdf"));
            PdfRendererBuilder builder = new PdfRendererBuilder();
            File fontFile = new File("C:\\1.ttf");
            Font font = Font.createFont(Font.TRUETYPE_FONT,fontFile);
            System.out.println(font.getFamily());
            builder.useFont(fontFile,"宋体");
            builder.useFastMode();
            builder.withHtmlContent(html, "C://");
            builder.toStream(fos);

            builder.run();
            long end = System.currentTimeMillis();

            System.out.println("Profiling case took " + (end - start) + " milliseconds.");

//            Files.createDirectories(Paths.get("D:/profiling/"));
//            Files.write(Paths.get("D:/profiling/"+title+".pdf"), baos.toByteArray());
        }catch(Exception e){
            e.printStackTrace();
            return 1;
        }
        return 0;
    }
}
